package business.impls.stores;

import business.interfaces.stores.*;
import business.interfaces.commons.DataProvider;
import business.interfaces.commons.Save;
import business.interfaces.commons.Printer;
import business.interfaces.classes.Abit;
import business.interfaces.classes.Spez;
import business.interfaces.classes.Reports;
import business.interfaces.classes.FBSReport;
import business.impls.commons.implDataProvider;
import business.impls.commons.jdbcTable;
import business.impls.commons.implPrinter;
import business.impls.classes.implReports;
import business.impls.classes.BaseControl;
import business.impls.classes.implFBSReport;
import business.impls.classes.buildRangeList;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 07.04.2005
 * Time: 12:03:32
 * To change this template use File | Settings | File Templates.
 */
public class implAbitSystem implements AbitSystem{

    private implDataProvider prov = null;
    private implVocabularyStore vStore = null;
    private implAbitStore abitStore = null;
    private implExamStore examStore = null;
    private Printer printer = null;
    private implSpezStore spezStore = null;
    private implReports report;
    private int UserID = -1;
    public String getHost() {
        return Host;
    }

    public void setHost(String host) {
        Host = host;
    }

    private String Host = "localhost";

    public void setPrintDocs(int print) {
        printer.setPrintDocs(print);
    }

    public void setPrinterOptionSee(boolean issee) {
        printer.setPrinterOptionSee(issee);
    }

    public void setPrint(Printer printer) {
        this.printer = printer;
    }


    public int InitAll() {
        this.setOperation("Init Data Base Driver.");
        this.prov = new implDataProvider();
        this.prov.setSQLDriver("com.mysql.jdbc.Driver");
        this.prov.setSQLUrl("jdbc:mysql://"+this.Host+"/abit");
        this.prov.setUserName("root");
        this.prov.setUSerPassword("qwertyz");
        this.setOperation("Opening connection and test connection link");
        if (this.prov.OpenConnection()!=0){
            this.initStatus="Connection DataBase Error";
            return 1;
        }
        this.setOperation("Vocabulary Init");
        vStore = new implVocabularyStore();
        vStore.setDataProvider(this.prov);
        vStore.setDataSystem(this);
        this.setOperation("Abit Store init");
        abitStore = new implAbitStore();
        abitStore.setDataProvider(prov);
        abitStore.setDataSystem(this);
        this.setOperation("Exam Store init");
        this.examStore = new implExamStore();
        this.examStore.setDataProvider(prov);
        this.examStore.setDataSystem(this);
        this.setOperation("Spez Store init");
        this.spezStore = new implSpezStore();
        this.spezStore.setDataProvider(prov);
        this.spezStore.setDataSystem(this);
        this.setPrint(new implPrinter());
        //this.printer.setTemplateDir("reports");
        this.printer.setDataProvider(prov);
        this.printer.setDataSystem(this);
        this.report = new implReports();
        this.report.setDataProvider(prov);
        this.report.setDataSystem(this);
        return 0;
    }

    private String initStatus = "OK";
    public String GetCurrentInitStatus() {
        return this.initStatus;
    }
    private String Operation = "idle";
    public void setOperation(String opr){
        synchronized(this.Operation){
            this.Operation = opr;
        }
    }
    public String GetCurrentOperation() {
        synchronized(this.Operation){
            return this.Operation;
        }
    }
    private int Status = 0;
    public boolean CheckUserName(String Name, String Password) {
        this.setOperation("Checking user name/password");
        jdbcTable tmp = this.getDataProvider().getSQLStore();
        tmp.setQuery("select Status,ID from users where (Name='"+Name+"') and (users.Pass=PASSWORD('"+Password+"'));");
        tmp.Prepare();
        if (tmp.getRowCount()>0){
            this.Status = (Integer)tmp.getValueAt(0,tmp.getColumnIndex("Status"));
            this.UserID = (Integer)tmp.getValueAt(0,tmp.getColumnIndex("ID"));
            return true;
        } else {return false;}
    }
    public int getStatus(){
        return this.Status;
    }
    public int getUserID(){
        return this.UserID;
    }
    public DataProvider getDataProvider() {
        return this.prov;
    }

    public void LogOut() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public AbitStore getAbitStore() {
        return this.abitStore;
    }

    public implExamStore getExamStore() {
        return this.examStore;
    }

    public SpezStore getSpezStore() {
        return this.spezStore;
    }

    public VocabularyStore getVocabularyStore() {
        return this.vStore;
    }

    private ArrayList<Save> for_save = new ArrayList<Save>();
    public void AddForSave(Save obj) {
        for (int i=0;i<for_save.size();i++){
            if (for_save.get(i).equals(obj)){return;}
        }
        for_save.add(obj);
    }

    public void SaveAll() {
        for (int i=0;i<for_save.size();i++){
            for_save.get(i).SaveAll();
        }
        for_save.clear();
    }
    private class printed{
        int abit;
        int sp;
    }
    private ArrayList<printed> for_print = new ArrayList<printed>();
    
    public void AddForPrint(Abit abit, Spez sp) {
        printed tmp = new printed();
        tmp.abit = abit.getID();
        if (sp!=null) {tmp.sp = sp.getSpezID();} else {tmp.sp = -1;}
        for (int i=0;i<for_print.size();i++){
            if ((sp!=null)){
            if ((for_print.get(i).abit==abit.getID())&&(for_print.get(i).sp==sp.getSpezID())&&(abit.getPrintForSpez(sp))){} else {abit.setPrintForSpez(sp);}} else
            {if (for_print.get(i).abit==abit.getID()&&(for_print.get(i).sp==-1)&&(abit.isIsnewAbit())) {} else {abit.setOldAbit();}}
        }
        for_print.add(tmp);
    }

    public ArrayList<Integer> getForPrint(Abit abit){
        ArrayList<Integer> tmp = new ArrayList<Integer>();
        ArrayList<Spez> tmpsp = this.getSpezStore().getSpezForAbit(abit);
        tmp.add(-1);
        for (int i=0;i<tmpsp.size();i++){
            tmp.add(tmpsp.get(i).getSpezID());
        }
        return tmp;
    }
    public ArrayList<Integer> getDefForPrint(){
        ArrayList<Integer> tmp = new ArrayList<Integer>();
        for (int i =0;i<this.for_print.size();i++){
            if (this.for_print.get(i).sp==-1){
                if (this.getAbitStore().getAbit(this.for_print.get(i).abit).isIsnewAbit()){tmp.add(this.for_print.get(i).sp);}
            } else {
                if (this.getAbitStore().getAbit(this.for_print.get(i).abit).getPrintForSpez(this.getSpezStore().getSpezbyID(this.for_print.get(i).sp))) {tmp.add(this.for_print.get(i).sp);}
            }
        }
        return tmp;

    }
    public void PrintAll(Abit abit,ArrayList<Integer> List) {
        for (int i =0;i<List.size();i++){
            if (List.get(i)==-1){
            printer.Print(abit);} else {
                printer.Print(abit,this.getSpezStore().getSpezbyID(List.get(i)));
            }
        }
    }

    public Reports getReports() {
        return this.report;
    }

    public Printer getPrinter() {
        return this.printer;
    }

    public BaseControl getBaseControl() {
        BaseControl tmp = new BaseControl();
        tmp.setDataSystem(this);
        tmp.setDataProvider(prov);
        return tmp;
    }

    public FBSReport getFBS() {
        implFBSReport tmp = new implFBSReport();
        tmp.setDataSystem(this);
        tmp.setDataProvider(prov);
        return tmp;
    }

    public buildRangeList getRangeList() {
        buildRangeList tmp = new buildRangeList();
        tmp.setDataSystem(this);
        tmp.setDataProvider(prov);
        return tmp;
    }
}
